//
//  LZCTools.m
//  CYWFrame
//
//  Created by Macx on 2017/7/25.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "LZCTools.h"
#import "UIView+LZCView.h"
#import "LoginViewController.h"
#import "UIView+LZCView.h"
#import "BaseNavViewController.h"

@implementation LZCTools

+ (int)thinkEnv {
    
    int state = 0;
    
    NSString *app = GetAppEnvironment(AppEn);
    NSString *app1 = GetAppEnvironmentBG(AppEnBG);

//    if ([GetUserDefaule(AppEnBG) isKindOfClass:[NSNumber class]]) {
//        
//        double delayInSeconds = 1.0;
//        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//            [self thinkEnv];
//            
//            return ;
//        });
//    }

//    if (!GetUserDefaule(AppEnBG)) {
//        return 0;
//    }
    
    if (!GetUserDefaule(AppEnBG)) {
		NSLog(@" i was here to refresh state");
        double delayInSeconds = 0.5f;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^{
            [self thinkEnv];
        });
        return 0;
    }
    
    // 两个都是0屏蔽功能，其余情况开启所有功能
    if ([app isEqualToString:@"0"] && [app1 isEqualToString:@"0"]) {
        state = 0;
    }
    else
        state = 1;
    
//    NSLog(@"==%d", state);
//    }
    
    return state;
}

/**
 获取文字Size
 
 @param text 文本
 @param font 文本字体
 @return Size
 */
+ (CGSize)textSizeWithText:(NSString *)text font:(UIFont *)font {
//    NSDictionary *attributes = @{NSFontAttributeName:font};
//    return [text boundingRectWithSize:CGSizeMake(MAXFLOAT, SCREEN_WIDTH/15) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    return [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, SCREEN_WIDTH/15)];
    
}


//CGRect rect = [self.content.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, NSIntegerMax) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.content.font} context:nil];


+ (UIAlertController *)alertMessage:(NSString *)str {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        if ([str isEqualToString:@"请先登录"] || [str isEqualToString:@"登录失败，请重新登录"]) {
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
            appDelegate.window.rootViewController = nav;
        }
        
    }]];
    
    return alert;
}


+ (UIAlertController *)AFNErrorAlert:(NSError *)error {
    
    NSString *message;
    
    if (error.code == -1009 || error.code == -1001) {
        message = @"请检查网络状况";
    }
    else {
		NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
		
		if (errorData) {
			NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];NSLog(@"error--%@", serializedData);
			message = serializedData[@"message"];
		}
		else {
			message = @"Sorry，未知错误，请稍后重试";
		}
    }
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    return alert;
}

+ (void)HUDMessage:(NSString *)msg delay:(CGFloat)delay {
    
    UIView *v = [[UIView alloc] init];
    
    UIViewController *currVC = [v topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:currVC.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    [hud hideAnimated:YES afterDelay:delay];
    double delayInSeconds = delay+2;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //执行事件
        [MBProgressHUD hideHUDForView:currVC.view animated:YES];
    });
    
}

@end
